﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Text;
using System.Net;
using System.IO;
using Facebook.Contracts;
using Facebook.Exceptions;
using Social.Core;
using Social.Core.Contracts;
using Social.Core.Protocols;

namespace Facebook.Channels
{
    /// <summary>
    /// This class represents the channel layer of this framework. 
    /// Based on a communication protocol, a set of configurations an a RequestFormatter is responsable to 
    /// provides means to format the http resquests and return the request response.
    /// </summary>
    /// <remarks>
    /// For usage samples consult the FacebookService implementation
    /// </remarks>
    public class FacebookChannel : IFacebookChannel
    {
        public FacebookChannel() { }


        private Facebookconfig config;

        private IRequestFormatter formatter ;

        private IProtocol protocol;

        public IProtocol Protocol
        {
            get { return protocol; }
            set { protocol = value;}
        }

        public FacebookChannel(Facebookconfig config)
        {
            this.config = config;
            this.formatter = config.RequestFormatter; 
        }
        
        public Facebookconfig Config
        {
            get { return config; }
            set { config = value; }
        }

        public IRequestFormatter RequestFormatter { get; set; }

        public string RequestGraph(string objectId, IDictionary<String, String> querystringparams)
        {
            if (config == null)
                throw new FacebookConfigException("no facebook configuration is instanciated");
            
            string url = string.Empty;
            if (querystringparams != null) 
            {
                // if its not an authentication reequest
              //  if (!objectId.Contains("oauth/"))
                    querystringparams.Add("access_token", this.config.AccessToken);
            }

            url = formatter.FormatUrl(config.ActionUrl, objectId, querystringparams);

            
            return  protocol.DoRequest(url);
            
        }


        public string QueryGraph(IDictionary<String, String> queryStringParams)
        {

            if (queryStringParams != null)
            {
                // if its not an authentication reequest
                queryStringParams.Add("access_token", this.config.AccessToken);
            }

            string url = formatter.FormatUrl(config.FqlUrl, null, queryStringParams);


            return protocol.DoRequest(url);
        }


        public string GetInsights(string metric, string since, string until)
        {
            if (config == null)
                throw new FacebookConfigException("no facebook configuration is instanciated");

            IDictionary<String, String> querystringparams = new Dictionary<String, String>();



            if (!String.IsNullOrWhiteSpace(since))
                querystringparams.Add("metric", metric);

            if(!String.IsNullOrWhiteSpace(since))
                querystringparams.Add("since", since);
            if(!String.IsNullOrWhiteSpace(until))
                querystringparams.Add("until", until);

            querystringparams.Add("access_token", this.config.AccessToken);


            string objectId = config.ApplicationID + "/insights" +( !string.IsNullOrWhiteSpace(metric) ? "/" + metric : ""); ;

            string url = formatter.FormatUrl(config.ActionUrl, objectId, querystringparams);

            return protocol.DoRequest(url);

        }

       
    }
}